<?php
namespace app\admin\controller;
class City
{
	public function __construct(){
		Admin_auth();
	}
	
    public function index()
    {	
		$type = input("get.type")?input("get.type"):'image';
		if(session('wmo2o_a_type')==1){
			$data = db("province")->order('id asc')->select();
		}else if(session('wmo2o_a_type')==2){
			$data = db("province")->whereIn('id',session('wmo2o_a_pid'))->order('id asc')->select();
		}
        return view('',['data'  => $data]);
    }
	
	public function ajax_get()
    {
		$type = input('get.type');
		$father_id = input('get.id');
		if(!empty($father_id)){
			$where = ['father_id'=>$father_id,'status'=>1];
		}else{
			$where = ['status'=>1];
		}
		if(session('wmo2o_a_type')==1){
			$data = db($type)->where($where)->order('id asc')->select();
		}else if(session('wmo2o_a_type')==2){
			$data = db($type)->where($where)->whereIn('id',session('wmo2o_a_pid'))->order('id asc')->select();
		}
		$return = array(
			'data' => $data
		);
		return $return;
    }
	
	public function status()
    {
		$type = input('get.type');
		$id = input('get.id');
		$status = input('get.status');
		switch ($type)
		{
		case 'province':
			if(session('wmo2o_a_type')==2){
				if(!in_array($id,explode(',',session('wmo2o_a_pid')))){
					break;
				}
			}
			db($type)->where(['id'=>$id])->setField('status',$status);
			db('city')->where(['father_id'=>$id])->setField('status',$status);
			$city = db('city')->where(['father_id'=>$id])->column('id');
			db('area')->whereIn('father_id',$city)->setField('status',$status);
			break;
		case 'city':
			if(session('wmo2o_a_type')==2){
				if(!in_array($id,explode(',',session('wmo2o_a_pid')))){
					break;
				}
			}
		   db($type)->where(['id'=>$id])->setField('status',$status);
		   db('area')->where(['father_id'=>$id])->setField('status',$status);
		   break;
		case 'area':
			if(session('wmo2o_a_type')==2){
				if(!in_array($id,explode(',',session('wmo2o_a_pid')))){
					break;
				}
			}
			db($type)->where(['id'=>$id])->setField('status',$status);
			break;
		default:
		    echo "No number between 1 and 3";
		}
		redirect(url('/admin/city'));
		exit;
    }
}
